package com.lyt.mall.order.web;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lyt.common.constant.OrderConstant;
import com.lyt.common.exception.NoStockException;
import com.lyt.mall.order.config.AlipayTemplate;
import com.lyt.mall.order.service.OrderService;
import com.lyt.mall.order.vo.OrderConfirmVO;
import com.lyt.mall.order.vo.OrderResponseVO;
import com.lyt.mall.order.vo.OrderSubmitVO;
import com.lyt.mall.order.vo.PayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author: LiuYutong
 */
@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;
    @Autowired
    AlipayTemplate alipayTemplate;

    @GetMapping("/toTrade")
    public String toTrade(Model model){
        //TODO 查询订单确认页需要的信息
        OrderConfirmVO confirmVO = orderService.confirmOrder();
        model.addAttribute("confirmVO",confirmVO);
        return "confirm";
    }

    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVO vo, Model model, RedirectAttributes redirectAttributes){
        Integer code = 0;
        OrderResponseVO responseVO = null;
        try{
            responseVO = orderService.submitOrder(vo);
            code = responseVO.getCode();
        }catch (NoStockException e){
            code = 2;
        }
        if (code == 0){
            model.addAttribute("orderResponseVO",responseVO);
            //表示下单操作成功
            return "pay";
        }else{
            System.out.println("code = " + code);
            String msg = "订单失败";
            if (code == 1){
                msg = msg + ":重复提交";
            }else if(code == 2){
                msg = msg + ":锁定库存失败";
            }
            //redirectAttributes.addAttribute("msg",msg);
            redirectAttributes.addFlashAttribute("msg",msg);
            //表示下单操作失败
            return "redirect:http://order.lyt.com/toTrade";
        }
    }

    @GetMapping("/orderPay/returnUrl")
    public String orderPay(@RequestParam(value = "orderSn",required = false) String orderSn,
                           @RequestParam(value = "out_trade_no",required = false) String out_trade_no){
        //TODO 完成相关的支付操作
        System.out.println("orderSn = " + orderSn);
        if (StringUtils.isNotBlank(orderSn)){
            orderService.handleOrderComplete(orderSn);
        }else {
            orderService.handleOrderComplete(out_trade_no);
        }
        return "list";
    }

    /**
     * 获取订单相关信息
     * 然后跳转到支付页面
     * @param orderSn
     * @return
     */
    @GetMapping(value = "/payOrder",produces = "text/html")
    @ResponseBody
    public String payOrder(@RequestParam("orderSn")String orderSn){
        //根据订单编号查询出相关的订单信息，封装到PayVO中
        PayVO payVO = orderService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVO);
        //System.out.println(pay);
        return pay;
    }
}
